import torch

x = torch.tensor(5.0,requires_grad=True)
y = x ** 3

y.backward(retain_graph=True)
print(x.grad)
x.grad.zero_()

y.backward(retain_graph=True)
print(x.grad)
x.grad.zero_()

y.backward(retain_graph=True)
print(x.grad)